<template>
	<view class="">
		<view class="uni-data-tree-cover" v-if="showCascade" @click="handleClose"></view>
		<view class="uni-data-tree-dialog" v-if="showCascade">
			<view class="uni-popper__arrow"></view>
			<view class="dialog-caption">
				<view class="title-area">
					<text class="dialog-title">{{ popupTitle }}</text>
				</view>
				<view class="dialog-close" @click="handleClose">
					<view class="dialog-close-plus" data-id="close"></view>
					<view class="dialog-close-plus dialog-close-rotate" data-id="close"></view>
				</view>
			</view>
			<view class="uni-data-pickerview">
				<view class="uni-data-pickerview-search" v-if="showSearch">
					<view class="uni-data-pickerview-search-input">
						<input type="text" v-model="keyword" :placeholder="searchPlaceholder"
							placeholder-class="uni-data-pickerview-search-input-placeholder" />
					</view>
					<view class="uni-data-pickerview-search-btn" @click="handleSearch">
						搜索
					</view>
				</view>
				<scroll-view class="selected-area" scroll-x="true" scroll-y="false" :show-scrollbar="false">
					<view class="selected-list">
						<template>
							<view class="selected-item selected-item-active"
								:class="{'selected-item-text-overflow': ellipsis}">
								<text class="">请选择</text>
							</view>
						</template>
					</view>
				</scroll-view>
				<view class="tab-c">
					<template v-for="(child, i) in dataList">
						<scroll-view class="list" :key="i" v-if="i==selectedIndex" :scroll-y="true"
							:refresher-enabled="refresherEnabled" :refresher-triggered="triggered"
							@refresherpulling="handleRefresherpulling" @scrolltolower="handleScrolltolower"
							@refresherrefresh="onRefresh" @refresherabort="onAbort">
							<checkbox-group @change="checkboxChange">
								<label v-for="(item, j) in child">
									<view class="item" :class="{'is-disabled': !!item.disable}"
										@click="handleNodeClick(item, i, j)">
										<view class="item-checkbox">
											<text v-if="!showNameOnly"
												class="item-text item-text-overflow">{{dataType === 'string' ? item : `${item[map.text]}_${item[map.value]}`}}</text>
											<text v-else
												class="item-text item-text-overflow">{{dataType === 'string' ? item : `${item[map.text]}_${item.materialNo}`}}</text>
										</view>
										<view class="check" v-if="isSelected(item, selected)"></view>
									</view>
									<template v-if="isMaterial">
										<view class="item-desc item-text-overflow item-text-overflow-desc"
											@click="handleNodeClick(item, i, j)">
											<view class="item-row">
												<view class="">
													规格：
												</view>
												<view class="">
													{{ item.materialModel }}
												</view>
											</view>
										</view>
									</template>
									<template v-if="showMaterialDetail">
										<view class="item-desc item-text-overflow item-text-overflow-desc"
											@click="handleNodeClick(item, i, j)">
											<view class="item-row">
												<view class="">
													箱号：
												</view>
												<view class="">
													{{ item.boxNo }}
												</view>
											</view>
											<view class="item-row" v-if="item.batchNo">
												<view class="">
													批次：
												</view>
												<view class="">
													{{ item.batchNo }}
												</view>
											</view>
											<view class="item-row">
												<view class="">
													规格：
												</view>
												<view class="">
													{{ item.materialSpec }}
												</view>
											</view>
											<view class="item-row">
												<view class="">
													数量：
												</view>
												<view class="item-row-input">
													<input  @click.stop type="digit" v-model="item.count"  /> <!-- @input="handleInput(item,$event)" --> 
												</view>
											</view>
										</view>
									</template>
								</label>
							</checkbox-group>
							<uni-load-more v-if="bottomLoading" class="load-more" :contentText="loadMore"
								status="loading">
							</uni-load-more>
							<view class="no-more" v-if="child.length && end">
								~ 到底了 ~
							</view>
							<!--  底部按钮  -->
							<view class="footerPanel" v-if="multiSelect">
								<view class="footerPanel_body">
									<button @click="handleConfirm()" class="footerBtn scan">确定
									</button>
								</view>
							</view>
						</scroll-view>
					</template>

					<view class="empty-image" v-if="dataList && !dataList.length">
						<image src="/static/img/empty.png"></image>
						暂无数据
					</view>

					<view class="loading-cover" v-if="loading">
						<uni-load-more class="load-more" :contentText="loadMore" status="loading"></uni-load-more>
					</view>
					<view class="error-message" v-if="errorMessage">
						<text class="error-text">{{errorMessage}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import dataPicker from "./cascade-picker.js"

	/**
	 * DataPickerview
	 * @description uni-data-pickerview
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
	 * @property {Array} localdata 本地数据，参考
	 * @property {Boolean} step-searh = [true|false] 是否分布查询
	 * @value true 启用分布查询，仅查询当前选中节点
	 * @value false 关闭分布查询，一次查询出所有数据
	 * @property {String|DBFieldString} self-field 分布查询当前字段名称
	 * @property {String|DBFieldString} parent-field 分布查询父字段名称
	 * @property {String|DBCollectionString} collection 表名
	 * @property {String|DBFieldString} field 查询字段，多个字段用 `,` 分割
	 * @property {String} orderby 排序字段及正序倒叙设置
	 * @property {String|JQLString} where 查询条件
	 */
	export default {
		name: 'CascadePicker',
		emits: ['nodeclick', 'change', 'datachange', 'update:modelValue', 'search', 'scrolltolower', 'refresherrefresh'],
		mixins: [dataPicker],
		props: {
			loading: {
				type: Boolean,
				default: true
			},
			managedMode: {
				type: Boolean,
				default: false
			},
			ellipsis: {
				type: Boolean,
				default: true
			},
			popupTitle: {
				type: String,
				default: ''
			},
			showSearch: {
				type: Boolean,
				default: true
			},
			searchPlaceholder: {
				type: String,
				default: ''
			},
			refresherEnabled: {
				type: Boolean,
				default: true
			},
			triggered: {
				type: Boolean,
				default: false
			},
			bottomLoading: {
				type: Boolean,
				default: false
			},
			multiSelect: {
				type: Boolean,
				default: false
			},
			showNameOnly: {
				type: Boolean,
				default: false
			},
			showMaterialDetail: {
				type: Boolean,
				default: false
			},
			end: {
				type: Boolean,
				default: false
			},
			checkCount: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				keyword: null,
				showCascade: false,
				dataType: 'object',
				isMaterial: false
				//scopeNum : 0
			}
		},
		created() {
			if (this.managedMode) {
				return
			}

			this.$nextTick(() => {
				this.load()
			})
			
		},
		  // mounted(item) {
			 // this.inputClick()
		  // },
		methods: {
			isSelected(key, selected) {
				return this.selected.find(option => {
					return this.dataType === 'object' ? option[this.map.value] === key[this.map.value] : option ===
						key
				})
			},
			/** 点击搜索 */
			handleSearch() {
				this.$emit('search', this.keyword)
			},
			/** 滑动到底部 */
			handleScrolltolower() {
				this.$emit('scrolltolower')
			},
			/** 下拉 */
			handleRefresherpulling() {
				this.$emit('refresherpulling')
			},
			onRefresh() {
				this.$emit('refresherrefresh')
			},
			onPropsChange() {
				this._treeData = []
				this.selectedIndex = 0
				this.load()
			},
			load() {
				if (this.isLocaldata) {
					this.loadData()
				} else if (this.dataValue.length) {
					this.getTreePath((res) => {
						this.loadData()
					})
				}
			},
			handleSelect(index) {
				this.selectedIndex = index
			},
			handleNodeClick(item, i, j) {
				if (item.disable) {
					return
				}
				const node = this.dataList[i][j]
				if (this.multiSelect) {
					if (this.dataType === 'object') {
						if (this.selected.length === 1 && this.selected[0].value === null) {
							this.selected.splice(i, 1, node)
						} else {
							const index = this.selected.findIndex(option => {
								return option[this.map.value] === node[this.map.value]
							})
							if (index > -1) {
								this.selected.splice(index, 1)
							} else {
								this.selected.push(node)
							}
						}
					} else {
						const index = this.selected.findIndex(option => {
							return option === node
						})
						if (index > -1) {
							this.selected.splice(index, 1)
						} else {
							this.selected.push(node)
						}
					}
				} else {
					this.selected = node
				}
				if (!this.multiSelect) {
					this.hide()
					this.$emit('change', this.selected)
				}
			},
			handleConfirm() {
				if (!this.selected || !this.selected.length) {
					uni.showToast({
						title: '请选择信息',
						icon: 'none'
					})
					return
				}
				if (this.checkCount && this.selected.some(v => !v.count || v.count === '0')) {
					uni.showToast({
						title: '数量不能为0',
						icon: 'none'
					})
					return
				}
				if(this.selected.some(v => v.count > v.currentCount)){
					uni.showToast({
						title: '退料数量不能大于领料或者补料数量',
						icon: 'none'
					})
					return
				}
				this.hide()
				this.$emit('change', this.selected)
			},
			updateData(data) {
				this.isMaterial = data.hasOwnProperty('isMaterial') ? data.isMaterial : false
				if (!this.showCascade) {
					this.keyword = null
					this.showCascade = true
				}
				this.dataType = typeof data.treeData[0]
				this._treeData = data.treeData
				if (data.hasOwnProperty('selected')) this.selected = data.selected

				if (!this._treeData.length) {
					this.dataList = []
					this.loadData()
				} else {
					this._updateBindData()
				}
			},
			onDataChange() {
				this.$emit('datachange')
			},
			onSelectedChange(node, isleaf) {
				if (isleaf) {
					this._dispatchEvent(node)
				}

				if (node) {
					this.$emit('nodeclick', node)
				}
			},
			_dispatchEvent(node) {
				this.$emit('change', this.selected)
			},
			handleClose() {
				this.hide()
			},
			hide() {
				this.showCascade = false
			},
			 // handleInput(item,e){
				 
			 // 	if (item.count > item.maxCount){
				// 	this.$nextTick(()=>{
			 // 			item.count = item.maxCount;
				// 	})
				// } else {
			 // 		this.$nextTick(()=>{
			 // 			item.count = item.count;
			 // 		})
			 // 	}	
			 //  }
		}
	}
</script>
<style>
	.empty-image {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.empty-image image {
		width: 100px;
		height: 100px;
	}

	.uni-data-tree {
		flex: 1;
		position: relative;
		font-size: 14px;
	}

	.error-text {
		color: #DD524D;
	}

	.input-value {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		flex-wrap: nowrap;
		font-size: 14px;
		/* line-height: 35px; */
		padding: 0 10px;
		padding-right: 5px;
		overflow: hidden;
		height: 35px;
		/* #ifdef APP-NVUE */
		/* #endif */
		box-sizing: border-box;
	}

	.input-value-border {
		border: 1px solid #e5e5e5;
		border-radius: 5px;
	}

	.selected-area {
		/* flex: 1; */
		overflow: hidden;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.load-more {
		/* #ifndef APP-NVUE */
		margin-right: auto;
		/* #endif */
		/* #ifdef APP-NVUE */
		width: 40px;
		/* #endif */
	}

	.selected-list {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: nowrap;
		/* padding: 0 5px; */
	}

	.selected-item {
		flex-direction: row;
		/* padding: 0 1px; */
		/* #ifndef APP-NVUE */
		white-space: nowrap;
		/* #endif */
	}

	.text-color {
		color: #333;
	}

	.placeholder {
		color: grey;
		font-size: 12px;
	}

	.input-split-line {
		opacity: .5;
	}

	.arrow-area {
		position: relative;
		width: 20px;
		/* #ifndef APP-NVUE */
		margin-bottom: 5px;
		margin-left: auto;
		display: flex;
		/* #endif */
		justify-content: center;
		transform: rotate(-45deg);
		transform-origin: center;
	}

	.input-arrow {
		width: 7px;
		height: 7px;
		border-left: 1px solid #999;
		border-bottom: 1px solid #999;
	}

	.uni-data-tree-cover {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, .4);
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		z-index: 100;
	}

	.uni-data-tree-dialog {
		position: fixed;
		left: 0;
		top: 20%;
		right: 0;
		bottom: 0;
		background-color: #FFFFFF;
		border-top-left-radius: 10px;
		border-top-right-radius: 10px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		z-index: 102;
		overflow: hidden;
		/* #ifdef APP-NVUE */
		width: 750rpx;
		/* #endif */
		font-size: 14px;
		color: #606266;
	}

	.dialog-caption {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		/* border-bottom: 1px solid #f0f0f0; */
	}

	.title-area {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		/* #ifndef APP-NVUE */
		margin: auto;
		/* #endif */
		padding: 0 10px;
	}

	.dialog-title {
		/* font-weight: bold; */
		line-height: 44px;
	}

	.dialog-close {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		padding: 0 15px;
	}

	.dialog-close-plus {
		width: 16px;
		height: 2px;
		background-color: #666;
		border-radius: 2px;
		transform: rotate(45deg);
	}

	.dialog-close-rotate {
		position: absolute;
		transform: rotate(-45deg);
	}

	.picker-view {
		flex: 1;
		overflow: hidden;
	}

	.icon-clear {
		display: flex;
		align-items: center;
	}

	/* #ifdef H5 */
	@media all and (min-width: 768px) {
		.uni-data-tree-cover {
			background-color: transparent;
		}

		.uni-data-tree-dialog {
			position: absolute;
			top: 55px;
			height: auto;
			min-height: 400px;
			max-height: 50vh;
			background-color: #fff;
			border: 1px solid #EBEEF5;
			box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
			border-radius: 4px;
			overflow: unset;
		}

		.dialog-caption {
			display: none;
		}

		.icon-clear {
			/* margin-right: 5px; */
		}
	}

	/* #endif */

	/* picker 弹出层通用的指示小三角, todo：扩展至上下左右方向定位 */
	/* #ifndef APP-NVUE */
	.uni-popper__arrow,
	.uni-popper__arrow::after {
		position: absolute;
		display: block;
		width: 0;
		height: 0;
		border-color: transparent;
		border-style: solid;
		border-width: 6px;
	}

	.uni-popper__arrow {
		filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
		top: -6px;
		left: 10%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #EBEEF5;
	}

	.uni-popper__arrow::after {
		content: " ";
		top: 1px;
		margin-left: -6px;
		border-top-width: 0;
		border-bottom-color: #fff;
	}

	/* #endif */

	.uni-data-pickerview {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		overflow: hidden;
		height: 100%;
	}

	.uni-data-pickerview-search {
		display: flex;
		align-items: center;
		margin: 8px 15px;
		justify-content: space-between;
	}

	.uni-data-pickerview-search-input {
		flex: 1;
		margin-right: 20px;
		border: 1px solid #DCDFE6;
		height: 30px;
		border-radius: 4px;
		padding: 0 10px;
	}

	.uni-data-pickerview-search-input-placeholder {
		font-size: 14px;
	}

	.uni-data-pickerview-search-input>>>uni-input {
		height: 30px;
		line-height: 30px;
	}

	.uni-data-pickerview-search-input>>>.uni-input-input {
		height: 30px;
		line-height: 30px;
	}

	.uni-data-pickerview-search-btn {
		width: 80px;
		font-size: 14px;
		height: 30px;
		border-radius: 4px;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FF4E74;
	}

	.error-text {
		color: #DD524D;
	}

	.loading-cover {
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(255, 255, 255, .5);
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		z-index: 1001;
	}

	.load-more {
		/* #ifndef APP-NVUE */
		margin: auto;
		/* #endif */
	}

	.error-message {
		background-color: #fff;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		padding: 15px;
		opacity: .9;
		z-index: 102;
	}

	/* #ifdef APP-NVUE */
	.selected-area {
		width: 750rpx;
	}

	/* #endif */

	.selected-list {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: nowrap;
		padding: 0 5px;
		border-bottom: 1px solid #f8f8f8;
	}

	.selected-item {
		margin-left: 10px;
		margin-right: 10px;
		padding: 12px 0;
		text-align: center;
		/* #ifndef APP-NVUE */
		white-space: nowrap;
		/* #endif */
	}

	.selected-item-text-overflow {
		width: 168px;
		/* fix nvue */
		overflow: hidden;
		/* #ifndef APP-NVUE */
		width: 6em;
		white-space: nowrap;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
		/* #endif */
	}

	.selected-item-active {
		border-bottom: 2px solid #FF4E74;
	}

	.selected-item-text {
		color: #FF4E74;
	}

	.tab-c {
		position: relative;
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		overflow: hidden;
	}

	.list {
		flex: 1;
		height: calc(80vh - 140px);
	}

	.footerPanel {
		height: 60px;


	}

	.footerPanel_body {
		left: 0;
		bottom: 0;
		width: calc(100% - 14px);
		display: flex;
		position: fixed;
		padding: 10px 7px;
	}

	.footerBtn {
		height: 40px;
		margin: 0 5px;
		display: flex;
		line-height: 1;
		font-size: 14px;
		border-radius: 4px;
		align-items: center;
		justify-content: center;
		box-shadow: 0 0 5px rgba(201, 201, 201, 0.5);
		color: #fff;
		background: #FF4E74;
		flex: 1;

	}

	.btn_icon {
		margin-right: 8px;
	}

	.item {
		padding: 12px 15px;
		/* border-bottom: 1px solid #f0f0f0; */
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
	}

	.item-checkbox {
		display: flex;
		align-items: center;
	}

	.item-desc {
		margin: 0 18px 0 18px;
		padding-bottom: 12px;
		font-size: 12px;
		border-bottom: 1px solid #F2F6FC;
	}

	.item-row {
		display: flex;
		align-items: center;
		margin-bottom: 4px;
	}

	.item-row-input {
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		padding: 2px 8px;
		box-sizing: border-box;
	}

	.item-row-input input {
		font-size: 12px;
	}

	.item-text-overflow-desc {
		width: 25em !important;
	}

	.is-disabled {
		opacity: .5;
	}

	.item-text {
		/* flex: 1; */
		color: #333333;
	}

	.item-text-overflow {
		width: 280px;
		/* fix nvue */
		overflow: hidden;
		/* #ifndef APP-NVUE */
		width: 20em;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
		/* #endif */
	}

	.check {
		margin-right: 5px;
		border: 2px solid #FF4E74;
		border-left: 0;
		border-top: 0;
		height: 12px;
		width: 6px;
		transform-origin: center;
		/* #ifndef APP-NVUE */
		transition: all 0.3s;
		/* #endif */
		transform: rotate(45deg);
	}
	
	.no-more {
		font-size: 12px;
		color: #999;
		width: 100%;
		text-align: center;
		height: 50px;
		line-height: 50px;
		padding-bottom: 10px;
	}
</style>
